using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace WPChanger
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmMain : System.Windows.Forms.Form
	{
		//CONST
		private const string FILENAME = "settings.xml";
		private const string WPNAME = "wallpaper.bmp";
		private const string DIRECTORY = "Directory";
		private const string SELECTDIR = "Select Dir";
		private const string CHANGEWP = "-change";

		private WPManager _WPManager = null;

		private System.Windows.Forms.NotifyIcon nfyMain;
		private System.Windows.Forms.ContextMenu ctxMain;
		private System.Windows.Forms.Timer timInterval;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.MenuItem mnuCurrent;
		private System.Windows.Forms.MenuItem mnuDirectory;
		private System.Windows.Forms.MenuItem mnuBrowser;
		private System.Windows.Forms.MenuItem mnuOptions;
		private System.Windows.Forms.MenuItem mnuAbout;
		private System.Windows.Forms.MenuItem mnuExit;
		private System.Windows.Forms.MenuItem mnuCopy;
		private System.Windows.Forms.MenuItem mnuMove;
		private System.Windows.Forms.MenuItem mnuDelete;
		private System.Windows.Forms.PictureBox picLogo;
		private System.Windows.Forms.MenuItem mnuTimer;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.SaveFileDialog saveWP;
		private System.ComponentModel.IContainer components;

		public frmMain()
		{
			InitializeComponent();
		}
		public frmMain(string[] Args)
		{
			InitializeComponent();
			if (Args.Length > 0)
			{
				if (Args[0].ToLower().Equals(CHANGEWP))
				{
					_WPManager.SetWP(_WPManager.Provider.getRandomWP());
				}
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
            this.nfyMain = new System.Windows.Forms.NotifyIcon(this.components);
            this.ctxMain = new System.Windows.Forms.ContextMenu();
            this.mnuCurrent = new System.Windows.Forms.MenuItem();
            this.mnuCopy = new System.Windows.Forms.MenuItem();
            this.mnuMove = new System.Windows.Forms.MenuItem();
            this.mnuDelete = new System.Windows.Forms.MenuItem();
            this.mnuDirectory = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.mnuBrowser = new System.Windows.Forms.MenuItem();
            this.mnuTimer = new System.Windows.Forms.MenuItem();
            this.mnuOptions = new System.Windows.Forms.MenuItem();
            this.mnuAbout = new System.Windows.Forms.MenuItem();
            this.mnuExit = new System.Windows.Forms.MenuItem();
            this.timInterval = new System.Windows.Forms.Timer(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.picLogo = new System.Windows.Forms.PictureBox();
            this.saveWP = new System.Windows.Forms.SaveFileDialog();
            ((System.ComponentModel.ISupportInitialize)(this.picLogo)).BeginInit();
            this.SuspendLayout();
            // 
            // nfyMain
            // 
            this.nfyMain.ContextMenu = this.ctxMain;
            this.nfyMain.Icon = ((System.Drawing.Icon)(resources.GetObject("nfyMain.Icon")));
            this.nfyMain.Text = "WPChanger";
            this.nfyMain.Visible = true;
            this.nfyMain.DoubleClick += new System.EventHandler(this.nfyMain_DoubleClick);
            // 
            // ctxMain
            // 
            this.ctxMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuCurrent,
            this.mnuDirectory,
            this.mnuBrowser,
            this.mnuTimer,
            this.mnuOptions,
            this.mnuAbout,
            this.mnuExit});
            this.ctxMain.Popup += new System.EventHandler(this.ctxMain_Popup);
            // 
            // mnuCurrent
            // 
            this.mnuCurrent.Index = 0;
            this.mnuCurrent.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuCopy,
            this.mnuMove,
            this.mnuDelete});
            this.mnuCurrent.Text = "Current";
            // 
            // mnuCopy
            // 
            this.mnuCopy.Index = 0;
            this.mnuCopy.Text = "Copy To..";
            this.mnuCopy.Click += new System.EventHandler(this.mnuCopy_Click);
            // 
            // mnuMove
            // 
            this.mnuMove.Index = 1;
            this.mnuMove.Text = "Move To..";
            this.mnuMove.Click += new System.EventHandler(this.mnuMove_Click);
            // 
            // mnuDelete
            // 
            this.mnuDelete.Index = 2;
            this.mnuDelete.Text = "Delete";
            this.mnuDelete.Click += new System.EventHandler(this.mnuDelete_Click);
            // 
            // mnuDirectory
            // 
            this.mnuDirectory.Index = 1;
            this.mnuDirectory.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1});
            this.mnuDirectory.Text = "Directory";
            //this.mnuDirectory.Select += new System.EventHandler(this.mnuDirectory_Select);
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.Text = "";
            // 
            // mnuBrowser
            // 
            this.mnuBrowser.Index = 2;
            this.mnuBrowser.Text = "Browser";
            this.mnuBrowser.Click += new System.EventHandler(this.mnuBrowser_Click);
            // 
            // mnuTimer
            // 
            this.mnuTimer.Index = 3;
            this.mnuTimer.RadioCheck = true;
            this.mnuTimer.Text = "Timer";
            this.mnuTimer.Click += new System.EventHandler(this.mnuTimer_Click);
            // 
            // mnuOptions
            // 
            this.mnuOptions.Index = 4;
            this.mnuOptions.Text = "Options";
            this.mnuOptions.Click += new System.EventHandler(this.mnuOptions_Click);
            // 
            // mnuAbout
            // 
            this.mnuAbout.Index = 5;
            this.mnuAbout.Text = "About";
            this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
            // 
            // mnuExit
            // 
            this.mnuExit.Index = 6;
            this.mnuExit.Text = "Exit";
            this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
            // 
            // label1
            // 
            this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.label1.Location = new System.Drawing.Point(4, 4);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(132, 33);
            this.label1.TabIndex = 0;
            this.label1.Text = "Program Created by Daniele Otti";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.label1.UseMnemonic = false;
            // 
            // picLogo
            // 
            this.picLogo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.picLogo.Image = ((System.Drawing.Image)(resources.GetObject("picLogo.Image")));
            this.picLogo.Location = new System.Drawing.Point(141, 5);
            this.picLogo.Name = "picLogo";
            this.picLogo.Size = new System.Drawing.Size(32, 32);
            this.picLogo.TabIndex = 1;
            this.picLogo.TabStop = false;
            // 
            // frmMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(178, 40);
            this.Controls.Add(this.picLogo);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmMain";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "WP Changer";
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.Load += new System.EventHandler(this.frmMain_Load);
            ((System.ComponentModel.ISupportInitialize)(this.picLogo)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] Args) 
		{
			Application.Run(new frmMain());
		}

		private void frmMain_Load(object sender, System.EventArgs e)
		{
			//_WPManager = new WPManager(this.timInterval,Application.StartupPath + @"\" + FILENAME,Application.StartupPath + @"\" + WPNAME );
			_WPManager = new WPManager(this.timInterval,Application.StartupPath + @"\" + WPNAME );
			_WPManager.mySettings = SettingsManager.loadSettings(Application.StartupPath + @"\" + FILENAME);
			if (!Directory.Exists(_WPManager.mySettings.WPDirectory))
			{
				_WPManager.mySettings.WPDirectory = Application.StartupPath;
			}
			_WPManager.Provider = new WPProvider(_WPManager.mySettings);
			this.HideApp();

			mnuDirectory.MenuItems.Clear();
			MenuItem mi = new MenuItem();
			foreach(string s in _WPManager.Provider.getFirstLevelDirectories())
			{
				mi = new MenuItem(s);
				mi.Click+=new EventHandler(mnuGeneric_Click);
				mnuDirectory.MenuItems.Add(mi);
			}
			foreach(MenuItem childMi in mnuDirectory.MenuItems)
			{
				System.Diagnostics.Debug.WriteLine(childMi.Text);
				this.genericMenuBuilder(childMi,childMi.Text);
			}
		}

		private void ctxMain_Popup(object sender, System.EventArgs e)
		{
		
		}

		private void mnuExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		private void mnuAbout_Click(object sender, System.EventArgs e)
		{
			this.ShowApp();
		}

		private void mnuOptions_Click(object sender, System.EventArgs e)
		{
			frmOptions fo = new frmOptions(_WPManager.mySettings,Application.StartupPath + @"\" + FILENAME, Application.ExecutablePath);
			fo.Show();
			
		}

		private void mnuBrowser_Click(object sender, System.EventArgs e)
		{
			//TODO: Add wallpaper browser
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			e.Cancel = true;
			this.WindowState = FormWindowState.Minimized;
			Hide();
		}

		public void HideApp()
		{
			this.WindowState = FormWindowState.Minimized;
			Hide();
		}

		public void ShowApp()
		{
			Show();
			this.WindowState = FormWindowState.Normal;
		}

		private void nfyMain_DoubleClick(object sender, System.EventArgs e)
		{
			_WPManager.SetWP(_WPManager.Provider.getRandomWP());
		}

		private void mnuCopy_Click(object sender, System.EventArgs e)
		{
			saveWP = new SaveFileDialog();
			saveWP.InitialDirectory = Application.StartupPath;
			//saveWP.Filter = "All Files (*.*)";

			if(saveWP.ShowDialog() == DialogResult.OK)
			{
				_WPManager.copyTo(saveWP.FileName,true);
			}
		}

		private void mnuMove_Click(object sender, System.EventArgs e)
		{
			saveWP = new SaveFileDialog();
			saveWP.InitialDirectory = Application.StartupPath;
			saveWP.Filter = "*.*";

			if(saveWP.ShowDialog() == DialogResult.OK)
			{
				_WPManager.moveTo(saveWP.FileName,true);
			}
		}

		private void mnuDelete_Click(object sender, System.EventArgs e)
		{
			_WPManager.delete();
			_WPManager.SetWP(_WPManager.Provider.getRandomWP());
		}

//		private void nfyMain_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
//		{
//			/*for (int i= tempMenu.MenuItems.Count -1; i>0 ; i--)
//			{
//				tempMenu.MenuItems.RemoveAt(i);
//			}*/
//
//			mnuDirectory.MenuItems.Clear();
//			/*if (mnuDirectory.MenuItems.Count > 1)
//			{
//				mnuDirectory.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
//																						this.menuItem1});
//			}
//			else
//			{
//				mnuDirectory.MenuItems.Clear();
//			}*/
//			//this.genericMenuDeleter(mnuDirectory);
//			MenuItem mi = new MenuItem();
//			/*
//			mi.Text = SELECTDIR;
//			mi.Click+=new EventHandler(mnuSelectDir_Click);
//			tempMenu.MenuItems.Add(mi);
//
//			tempMenu.MenuItems.Add(new MenuItem("-"));
//			*/
//			foreach(string s in _WPManager.Provider.getFirstLevelDirectories())
//			{
//				mi = new MenuItem(s);
//				mi.Click+=new EventHandler(mnuGeneric_Click);
//				mnuDirectory.MenuItems.Add(mi);
//			}
//			foreach(MenuItem childMi in mnuDirectory.MenuItems)
//			{
//				System.Diagnostics.Debug.WriteLine(childMi.Text);
//				this.genericMenuBuilder(childMi,childMi.Text);
//			}
//			//this.genericMenuBuilder(mnuDirectory);	
//		}

		/*private void genericMenuDeleter(System.Windows.Forms.MenuItem tempMenu)
		{
			foreach(MenuItem mi in tempMenu.MenuItems)
			{
				this.genericMenuDeleter(mi);
			}
			tempMenu.Click-=null;
		}*/

		private void genericMenuBuilder(System.Windows.Forms.MenuItem tempMenu, string folderString)
		{
			foreach(string s in _WPManager.Provider.getSubLevelDirectories(folderString))
			{
				MenuItem mi = new MenuItem(s);
				mi.Click+=new EventHandler(mnuGeneric_Click);
				tempMenu.MenuItems.Add(mi);
			}
			foreach(MenuItem childMi in tempMenu.MenuItems)
			{
				System.Diagnostics.Debug.WriteLine(childMi.Text);
				this.genericMenuBuilder(childMi,folderString + @"\" + childMi.Text);
			}
		}

		/*private void mnuSelectDir_Click(object sender, System.EventArgs e)
		{
			ArrayList dirList = new ArrayList();
			MenuItem tempMenu = ((MenuItem)sender);
			while (tempMenu.Parent != mnuDirectory)
			{
				dirList.Add(tempMenu.Text);
				tempMenu = (MenuItem)tempMenu.Parent;
			}
			string dir = _WPManager.Provider.CurrentDir;
			for (int i=dirList.Count -1; i>0;i--)
			{
				dir+=@"\"+dirList[i];
			}
			_WPManager.Provider.changeWPDir(dir);
		}*/

       /* private void mnuDirectory_Select(object sender, EventArgs e)
        {

        }	*/

		private void mnuGeneric_Click(object sender , System.EventArgs e)
		{
			//this.genericMenuBuilder((MenuItem)sender);
			ArrayList dirList = new ArrayList();
			MenuItem tempMenu = ((MenuItem)sender);

            if (tempMenu.Parent.MenuItems[tempMenu.Index].MenuItems.Count > 0)
            {
            }
                while (tempMenu.Parent.MenuItems[tempMenu.Index] != mnuDirectory)
                {
                    dirList.Add(tempMenu.Text);
                    tempMenu = (MenuItem)tempMenu.Parent;
                }
                //string dir = _WPManager.Provider.CurrentDir;
                string dir = "";
                for (int i = dirList.Count - 1; i >= 0; i--)
                {
                    dir += dirList[i] + @"\";
                }
            
			_WPManager.Provider.changeWPDir(dir.Substring(0,dir.Length -1));
		}

		private void mnuTimer_Click(object sender, System.EventArgs e)
		{
			mnuTimer.Checked = !mnuTimer.Checked;
			if (mnuTimer.Checked == true)
			{
				_WPManager.enableTimer();
			}
			else
			{
				_WPManager.disableTimer();
			}
		}
	}
}
